package com.company.woye.taiorcheckSize;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.checkmakesure.bean.SuccessBean;
import com.company.woye.internet.KjjPHCallBack;
import com.company.woye.taiorcheckSize.adapter.TaiorListviewAddAdapter;
import com.company.woye.taiorcheckSize.bean.CheckBean;
import com.company.woye.taiorcheckSize.bean.CheckSizeBean;
import com.company.woye.taiorcheckquestion.ChangeQuestionNumActivity;
import com.company.woye.taiorcheckquestion.bean.QuestionBean;
import com.company.woye.utils.BasePopupWindow;
import com.company.woye.utils.Constans;
import com.company.woye.utils.Toaster;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

public class ChangeSizeActivity extends BaseActivity {


    PopupWindow popupWindow;
    View inflaterView;
    double Size = 0d;
    @BindView(R.id.iv_on_package_back)
    ImageView ivOnPackageBack;
    @BindView(R.id.tv_on_package_back)
    TextView tvOnPackageBack;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_refresh)
    TextView tvRefresh;
    @BindView(R.id.tv_LSX)
    TextView tvLSX;
    @BindView(R.id.tv_slfz)
    TextView tvSlfz;
    @BindView(R.id.tv_slfh)
    TextView tvSlfh;
    @BindView(R.id.tv_ypcc)
    TextView tvYpcc;
    @BindView(R.id.tv_ypcczreal)
    TextView tvYpcczreal;
    @BindView(R.id.tv_ypccadd)
    TextView tvYpccadd;
    @BindView(R.id.tv_ypccplus)
    TextView tvYpccplus;
    @BindView(R.id.tv_ypcch)
    TextView tvYpcch;
    @BindView(R.id.tv_ypcchreal)
    TextView tvYpcchreal;
    @BindView(R.id.tv_ypcchadd)
    TextView tvYpcchadd;
    @BindView(R.id.tv_ypcchplus)
    TextView tvYpcchplus;
    @BindView(R.id.tv_jccbjz)
    TextView tvJccbjz;
    @BindView(R.id.tv_jccbjh)
    TextView tvJccbjh;
    @BindView(R.id.tv_num_add)
    TextView tvNumAdd;
    @BindView(R.id.tv_num_plus)
    TextView tvNumPlus;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.rl)
    RelativeLayout rl;
    String type = "";//2衣片尺寸横  1 衣片尺寸直
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String cjName, txm, bmName, bw, cm, token, hyh, lsx,inputId="",inputMan="";
    CheckSizeBean.DataBean.ZhiCjypcsSizeSubBean zhiCjypcsSizeSub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_change_size;
    }

    @Override
    public void initView() {
        cjName = getIntent().getStringExtra("cjName");
        txm = getIntent().getStringExtra("txm");
        bmName = getIntent().getStringExtra("bmName");
        bw = getIntent().getStringExtra("bw");
        cm = getIntent().getStringExtra("cm");
        hyh = getIntent().getStringExtra("hyh");
        lsx = getIntent().getStringExtra("lsx");
        token = getIntent().getStringExtra("token");
        inputId = getIntent().getStringExtra("inputId");
        inputMan = getIntent().getStringExtra("inputMan");
        tvLSX.setText(bw);
        /**
         *   "bmName": "制衣二部(1)",
         *   "cjName": "裁剪一工场",
         *   "txm": "276000534396",
         *   "bw": "1后片:1片",
         *   "cm": "M",
         *   "hyh": "NC07015-A08BOR",
         *   "token": "7e0daaaed1a1543727122788a4e5398e7c23e88b075a32e3922a6d96b5642db9"
         * }
         */
        getDetail(cjName, txm, bmName, token, bw, cm, hyh);
//        getDetail("裁剪一工场", "276000534396", "制衣二部(1)", "7e0daaaed1a1543727122788a4e5398e7c23e88b075a32e3922a6d96b5642db9" +
//                "", "1后片:1片", "M", "NC07015-A08BOR");
    }

    private void getDetail(String cjName, String txm, String bmName, String token, String bw, String cm, String hyh) {
        JSONObject params = new JSONObject();
        try {
            params.put("cjName", cjName);
            params.put("txm", txm);
            params.put("bmName", bmName);
            params.put("token", token);
            params.put("bw", bw);
            params.put("cm", cm);
            params.put("hyh", hyh);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("提交数据-1", params.toString());
        Log.e("访问地址-2", Constans.IP + "szapi/third/BdMlcj/listCjypmlccjy");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json"))
                .content(params.toString()).url(Constans.IP + "szapi/third/BdMlcj/listCjypmlccjy").build().execute(new KjjPHCallBack<CheckSizeBean>(CheckSizeBean.class, ChangeSizeActivity.this, true) {
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(CheckSizeBean response, int id) {
                System.out.println("onResponse:"+response);
                Toaster.showLong(ChangeSizeActivity.this, response.getMsg());
                tvYpcc.setText("0");
                tvYpcczreal.setText("0");
                tvJccbjz.setText("0");
                tvYpcch.setText("0");
                tvYpcchreal.setText("0");
                tvJccbjh.setText("0");
                tvSlfh.setText("0");
                tvSlfz.setText("0");
                if (response.getMsg()!=null&&response.getMsg().equals("success")) {
                    //填充数据
                    System.out.println("onResponse----A");
                    if (response.getData()!=null&&response.getData().getZhiCjypcsSizeSub()!=null) zhiCjypcsSizeSub = response.getData().getZhiCjypcsSizeSub();
                    System.out.println("onResponse----B");
                    if (response.getData()!=null&&response.getData().getJs()!=null) tvNum.setText(response.getData().getJs());
                    System.out.println("onResponse----C");
                    if (zhiCjypcsSizeSub!=null){
                        if (zhiCjypcsSizeSub.getZhiDef()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getZhiDef())) {
                            if (zhiCjypcsSizeSub.getZhiDef().length()>0) tvYpcc.setText(zhiCjypcsSizeSub.getZhiDef());
                            else tvYpcc.setText("0");
                        } else {
                            tvYpcc.setText("0");
                        }
                        if (zhiCjypcsSizeSub.getZhi()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getZhi())) {
                            if (zhiCjypcsSizeSub.getZhi().length()>0) tvYpcczreal.setText(zhiCjypcsSizeSub.getZhi());
                            else tvYpcczreal.setText("0");
                        } else {
                            tvYpcczreal.setText("0");
                        }
                        if (zhiCjypcsSizeSub.getZhiRes()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getZhiRes())) {
                            if (zhiCjypcsSizeSub.getZhiRes().length()>0)  tvJccbjz.setText(zhiCjypcsSizeSub.getZhiRes());
                            else tvJccbjz.setText("0");
                        } else {
                            tvJccbjz.setText("0");
                        }


                        if (zhiCjypcsSizeSub.getHengDef()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getHengDef())) {
                            if (zhiCjypcsSizeSub.getHengDef().length()>0)   tvYpcch.setText(zhiCjypcsSizeSub.getHengDef());
                            else tvYpcch.setText("0");
                        } else {
                            tvYpcch.setText("0");
                        }
                        if (zhiCjypcsSizeSub.getHeng()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getHeng())) {
                            if (zhiCjypcsSizeSub.getHeng().length()>0)  tvYpcchreal.setText(zhiCjypcsSizeSub.getHeng());
                            else tvYpcchreal.setText("0");
                        } else {
                            tvYpcchreal.setText("0");
                        }

                        if (zhiCjypcsSizeSub.getHengRes()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getHengRes())) {
                            if (zhiCjypcsSizeSub.getHengRes().length()>0)   tvJccbjh.setText(zhiCjypcsSizeSub.getHengRes());
                            else tvJccbjh.setText("0");
                        } else {
                            tvJccbjh.setText("0");
                        }

                        if (zhiCjypcsSizeSub.getHsl()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getHsl())) {
                            if (zhiCjypcsSizeSub.getHsl().length()>0)  tvSlfh.setText(zhiCjypcsSizeSub.getHsl());
                            else tvSlfh.setText("0");
                        } else {
                            tvSlfh.setText("0");
                        }
                        if (zhiCjypcsSizeSub.getZsl()!=null&&!TextUtils.isEmpty(zhiCjypcsSizeSub.getZsl())) {
                            if (zhiCjypcsSizeSub.getZsl().length()>0)   tvSlfz.setText(zhiCjypcsSizeSub.getZsl());
                            else  tvSlfz.setText("0");
                        } else {
                            tvSlfz.setText("0");
                        }

                    }else {
                        Toast.makeText(ChangeSizeActivity.this,"没有部位信息",Toast.LENGTH_LONG).show();
                    }

                }
            }
        });
    }

    @OnClick({R.id.iv_on_package_back, R.id.tv_on_package_back, R.id.tv_commit, R.id.tv_refresh, R.id.tv_ypccadd, R.id.tv_ypccplus, R.id.tv_ypcchadd, R.id.tv_ypcchplus, R.id.tv_num_add, R.id.tv_num_plus})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_on_package_back:
            case R.id.tv_on_package_back:
                finish();
                break;
            case R.id.tv_commit:
                //保存
                commit();
                break;
            case R.id.tv_refresh:
                sendBroadcast(new Intent("finish"));
                finish();
                break;
            case R.id.tv_ypccadd:
            case R.id.tv_ypccplus:
                type = "1";
                showPopWindows(Double.parseDouble(tvYpcczreal.getText().toString()));
                break;
            case R.id.tv_ypcchadd:
            case R.id.tv_ypcchplus:
                type = "2";
                showPopWindows(Double.parseDouble(tvYpcchreal.getText().toString()));
                break;
            case R.id.tv_num_add:
                if (!TextUtils.isEmpty(tvNum.getText().toString())) {
                    int num = Integer.parseInt(tvNum.getText().toString());
                    num++;
                    tvNum.setText(num + "");
                }
                break;
            case R.id.tv_num_plus:
                if (!TextUtils.isEmpty(tvNum.getText().toString())) {
                    int num = Integer.parseInt(tvNum.getText().toString());
                    if (num > 0) {
                        num--;
                        tvNum.setText(num + "");
                    } else {
                        Toast.makeText(ChangeSizeActivity.this, "不能再减了", Toast.LENGTH_LONG).show();
                    }

                }
                break;
        }
    }

    private void commit() {
        /**
         * {
         *   "bbm": "qui aute anim consequat",   "cjName": "elit mollit aliquip",    "lsx": "culpa elit deserunt minim sint",    "bmName": "anim",
         *   "inputId": "ex fugiat"   "inputMan": "minim sed",   "js": "eiusmod sit est voluptate aliquip",   "zhiDef": "fugiat aute", "zhi": "Ut cupidatat",
         *   "zhiRes": -52601437.2583345   "hengDef": 33926031.25307003,   "heng": -48608829.094977856,   "hengRes": 1641001.8393303752,
         *   "hsl": "sit proident",   "zsl": "Duis nostrud",   "jieguo": "deserunt dolor",   "txm": "ea ut ex",   "cm": null,  "bw": "commodo sint irure ullamco officia",
         *   "token": "eu aute"
         * }
         */
        String bbm = bmName.substring(0, 4);
        String js = tvNum.getText().toString();

        String zhiDef=tvYpcc.getText().toString();
        String zhi=tvYpcczreal.getText().toString();
        String zhiRes=tvJccbjz.getText().toString();

        String hengDef=tvYpcch.getText().toString();
        String heng=tvYpcchreal.getText().toString();
        String hengRes=tvJccbjh.getText().toString();

        String hsl=tvSlfh.getText().toString();
        String zsl=tvSlfz.getText().toString();

        String jieguo=tvResult.getText().toString();


        JSONObject params = new JSONObject();
        try {
            params.put("bbm", bbm);
            params.put("cjName", cjName);
            params.put("bmName", bmName);
            params.put("lsx", lsx);
            params.put("bmName", bmName);
            params.put("inputId", inputId);
            params.put("inputMan", inputMan);
            params.put("js", js);
            params.put("zhiDef", zhiDef);
            params.put("zhi", zhi);
            params.put("zhiRes", zhiRes);
            params.put("hengDef", hengDef);
            params.put("heng", heng);
            params.put("hengRes", hengRes);
            params.put("hsl", hsl);
            params.put("zsl", zsl);
            params.put("jieguo", jieguo);
            params.put("txm", txm);
            params.put("cm", cm);
            params.put("bw", bw);
            params.put("token", token);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("提交数据", params.toString());
        Log.e("访问地址", Constans.IP + "szapi/third/BdMlcj/saveCjypmlccjy");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json"))
                .content(params.toString()).url(Constans.IP + "szapi/third/BdMlcj/saveCjypmlccjy").build().execute(new KjjPHCallBack<SuccessBean>(SuccessBean.class, ChangeSizeActivity.this, true) {
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(SuccessBean response, int id) {
                Toaster.showLong(ChangeSizeActivity.this, response.getMsg());
                if (response.getMsg().equals("success")) {
                    finish();
                }
            }
        });


    }

    //显示弹窗
    private void showPopWindows(Double size) {
        Size = 0;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //创建并初始化完成
        //创建并初始化完成
        if (popupWindow == null) {
            popupWindow = new BasePopupWindow(this);
            popupWindow.setWidth(dm.widthPixels / 3);
            popupWindow.setHeight(dm.heightPixels * 3 / 5);
        }
        //取视图
        if (inflaterView == null)
            inflaterView = LayoutInflater.from(this).inflate(R.layout.level_pop_size, null);
        TextView tv_add = inflaterView.findViewById(R.id.tv_add);
        TextView tvreal = inflaterView.findViewById(R.id.tv_real_size);
        tvreal.setText(size + "");
        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvreal.setText((size + Size) + "");
            }
        });
        TextView tv_plus = inflaterView.findViewById(R.id.tv_plus);
        tv_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvreal.setText((size - Size) + "");
            }
        });
        ListView lv = inflaterView.findViewById(R.id.lv);
        List<CheckBean> checkString = new ArrayList<>();
        checkString.add(new CheckBean("±0", false));
        checkString.add(new CheckBean("±0.5", false));
        checkString.add(new CheckBean("±1", false));
        checkString.add(new CheckBean("±1.5", false));
        checkString.add(new CheckBean("±2", false));
        TaiorListviewAddAdapter addAdapter = new TaiorListviewAddAdapter(ChangeSizeActivity.this, checkString);
        lv.setAdapter(addAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < checkString.size(); i++) {
                    checkString.get(i).setIscheck(false);
                }
                checkString.get(position).setIscheck(true);
                addAdapter.notifyDataSetChanged();
                Size = Double.parseDouble(checkString.get(position).getName().substring(1, checkString.get(position).getName().length()));

            }
        });

        inflaterView.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        inflaterView.findViewById(R.id.tv_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("1")) {
                    tvYpcczreal.setText(tvreal.getText().toString());
                    if (!TextUtils.isEmpty(tvYpcc.getText().toString()) && !TextUtils.isEmpty(tvYpcczreal.getText().toString())) {
                        tvJccbjz.setText((Double.parseDouble(tvYpcczreal.getText().toString()) - Double.parseDouble(tvYpcc.getText().toString())) + "");
                    }

                } else if (type.equals("2")) {
                    tvYpcchreal.setText(tvreal.getText().toString());
                    if (!TextUtils.isEmpty(tvYpcch.getText().toString()) && !TextUtils.isEmpty(tvYpcchreal.getText().toString())) {
                        tvJccbjh.setText((Double.parseDouble(tvYpcchreal.getText().toString()) - Double.parseDouble(tvYpcch.getText().toString())) + "");
                    }
                }
                if (!TextUtils.isEmpty(tvSlfh.getText().toString()) && !TextUtils.isEmpty(tvSlfz.getText().toString())
                        && !TextUtils.isEmpty(tvJccbjh.getText().toString()) && !TextUtils.isEmpty(tvJccbjz.getText().toString())
                        && !TextUtils.isEmpty(tvYpcc.getText().toString()) && !TextUtils.isEmpty(tvYpcch.getText().toString())) {
                    if (Math.abs(Double.parseDouble(tvJccbjh.getText().toString())) / Double.parseDouble(tvYpcch.getText().toString()) <= 1 - Double.parseDouble(tvSlfh.getText().toString()) / 100 &&
                            Math.abs(Double.parseDouble(tvJccbjz.getText().toString())) / Double.parseDouble(tvYpcc.getText().toString()) <= 1 - Double.parseDouble(tvSlfz.getText().toString())) {
                        tvResult.setText("合格");
                        tvResult.setTextColor(getResources().getColor(R.color.green));
                    } else {
                        tvResult.setText("不合格");
                        tvResult.setTextColor(getResources().getColor(R.color.red));
                    }
                } else {
                    tvResult.setTextColor(getResources().getColor(R.color.red));
                    tvResult.setText("不合格");
                }
                popupWindow.dismiss();
            }
        });
        popupWindow.setContentView(inflaterView);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//全透明背景
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(tvCommit, Gravity.CENTER, 0, 0);
    }
}
